/**
 * Created by weng on 2017/9/3.
 * 投票提问组件
 *
 */


define(function (require) {

    //创建websocket 用于没有统一的时候调试 调试结束后删除即可。
    var wsocket = require("webSocketChannel");



    function isMobile() {
        var _ua = window.navigator.userAgent,
            _isMobile = /(mobile|mobi|wap|iphone)/i.test(_ua),
            _isAndroid = /android/i.test(_ua),
            _isIpad = /(ipad)/i.test(_ua);
        if (_isMobile || _isAndroid || _isIpad) {
            return true;
        } else {
            return false;
        }
    }
    var $ = require('jquery')
    require("jquery.cookie");
    wsocket.onopen = function () {
        console.log('ws open');
    };
    var cv = require('courseViewController');
    var q_answer =cv._question;
    wsocket.onmessage = function (evt)
    {
        var received_msg = JSON.parse(evt.data);
        console.log(received_msg);
        if(received_msg.type == 'question-publish'){//发布问题
            q_answer.push({id:received_msg.data.id,answerCount:{}});
            if(isMobile()){
                cv.m_studentGetVote($.cookie('courseId'));
            }else{
                if($.cookie('type')=='student'){
                    var center_side = document.getElementById('center-side').innerHTML;
                    var list = `<div class="card" data-idx=${received_msg.data.id}>
                                <div class="card-title big-font">${received_msg.data.question}</div>
                                <div class="card-content">
                                    <ul class="options options-s">
                                        <input class="answer_input" type="text" placeholder="请输入你的答案">
                                        <img class="up_answer" src="image/upload.png" alt="">
                                </ul>
                                </div>
                            </div>`+center_side;
                    document.getElementById('center-side').innerHTML = list;
                    cv.eventBinding();
                }
            }


        }else if(received_msg.type =='send-answer'){
            console.log(q_answer);
            for(var i =0;i<q_answer.length;i++){
                if(q_answer[i].id==received_msg.data.questionId){
                    var a = false;
                    for(var k=0;k<q_answer[i].answerCount.length;k++){

                        if(q_answer[i].answerCount[k].answer==received_msg.data.answer){
                            a = true;
                            q_answer[i].answerCount[k].count++
                        }
                    }
                    if(a==false){
                        if(!Array.isArray(q_answer[i].answerCount)){
                            q_answer[i].answerCount = new Array();
                        }
                        console.log(typeof q_answer[i].answerCount)
                        q_answer[i].answerCount.push({answer:received_msg.data.answer,count:1})


                    }
                    var a_list = '';
                    console.log(q_answer[i].answerCount,i,typeof q_answer[i].answerCount);
                    q_answer[i].answerCount.map((item,index)=>{
                        a_list+=`<li><span>${item.answer}</span>${item.count}人</li>`;
                        console.log(item,index);
                    })

                    $(`#center-side .card[data-idx=${q_answer[i].id}] .options`).html(a_list)
                }
            }
        }
    };


    wsocket.onclose = function(e)
    {
        // 关闭 websocket
        console.log(e.type);
        // alert("连接已关闭...");
    };
    wsocket.onerror =function (e) {
        wsocket.close();
        wsocket =require("webSocketChannel");
        console.log('reconnect');
    }



    window.onunload =function () {
        wsocket.close();

    }
    setInterval(function () {
        wsocket.send('{"type":"heart-beat"}');
        // console.log(wsocket);
    },30000);










})